package com.bytes32.parsing;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

import com.bytes32.classification.tree.dataset.AttributeType;
import com.bytes32.classification.tree.dataset.FeatureType;
/**
 * Every successful intoText and intoNumber scores 1 point 
 * in the weight
 *
 * This will score 2 points for ordered
 * as it is able to parse intoNumber and intoText
 * 
 * This will score no points for unordered (assumed not number)
 * as it will fail both intoNumber and intoText
 * @author murariuf
 *
 */
public class DoubleParser extends AbstractParser<String>{

	@Override
	public Double intoNumber(String value) throws ParsingException {
		NumberFormat format = DecimalFormat.getInstance();
		if (value == null )
			throw new ParsingException(" unable to parse null value ");
		try {
			Number num = format.parse(value.trim());
			return num.doubleValue();
		} catch (ParseException e) {
			throw new ParsingException(e);
		}
	}

	@Override
	public String intoText(String value) throws ParsingException {
		if (value == null)
			throw new ParsingException("Cannot parse null value");
		return intoNumber(value).toString(); /*make sure we are are number*/
	}


	@Override
	public AttributeType getAttributeType() {
		return AttributeType.DOUBLE;
	}

	@Override
	public FeatureType getFeatureType() {
		return FeatureType.ORDERED;
	}
	
}
